package com.eaoa.uc.pubmessage.bo;

import com.eaoa.base.EABaseBusinessObject;
import com.eaoa.constants.PubBillConstants;
import com.eaoa.constants.PubMessageConstants;
import com.eaoa.dao.pub.PubMessageDAO;
import com.eaoa.vo.pub.PubMessage;
import com.stony.core.keygenerator.FillCodeBO;

public class MessageModifyBO extends EABaseBusinessObject {

	private Long messageno;
	private Long sessionid;
	private PubMessage vo;
	private boolean defaultvalues = true; // use default values 
	private static PubMessageDAO messageDAO = new PubMessageDAO();

	public MessageModifyBO(PubMessage vo) {
		super();
		this.vo = vo;
	}
	
	public MessageModifyBO(PubMessage vo, boolean defaultvalues) {
		super();
		this.vo = vo;
		this.defaultvalues = defaultvalues;
	}
 
	protected void performBusinessLogic() {

		if (vo.getMessageno() == null) {
			FillCodeBO codeBO = new FillCodeBO(PubBillConstants.BILLCODE_MESSAGE_SESSIONID);
			codeBO.execute();
			sessionid = codeBO.getCurrentno();
			vo.setSessionid(sessionid);
			
			if(defaultvalues){
				vo.setSender(getEmployee().getAccountno());
				vo.setSendername(getEmployee().getName());
				vo.setStatus(PubMessageConstants.MESSAGE_STATUS_NEW);
				
			}

			vo.setIslock("N");
			this.messageno = messageDAO.save(vo);

		} else {
			sessionid = vo.getSessionid();
			messageno = vo.getMessageno();
			PubMessage po = (PubMessage) messageDAO.loadAndLock(messageno);
			po.setMessagetype(vo.getMessagetype());
			po.setReceivers(vo.getReceivers());
			po.setReceivername(vo.getReceivername());
			po.setSubject(vo.getSubject());
			po.setContent(vo.getContent());

			messageDAO.update(po);

		}
	}


	public Long getMessageno() {
		return messageno;
	}
	
	public Long getSessionid() {
		return sessionid;
	}

}
